package com.net.shard.manager.impl;

import com.net.shard.dao.ShardConfigMapper;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.OrderCondition;
import com.net.common.manager.AbstractManager;
import com.net.shard.domain.ShardConfig;
import com.net.shard.manager.ShardConfigManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**

 */
@Component
public class ShardConfigManagerImpl extends AbstractManager<ShardConfig> implements ShardConfigManager {
    @Autowired
    private ShardConfigMapper mapper;

    @Override
    protected BasicMapper<Long, ShardConfig> getMapper() {
        return mapper;
    }

    @Override
    public List<ShardConfig> listShardConfig(String dataSourceName) {
        ShardConfig condition = new ShardConfig();
        condition.setDataSourceName(dataSourceName);

        return nonEmptyList(mapper.selectByCondition(condition, new ArrayList<OrderCondition>()));
    }

    @Override
    public ShardConfig findByShardId(String dataSourceName, Long shardId) {
        ShardConfig condition = new ShardConfig();
        condition.setDataSourceName(dataSourceName);
        condition.setShardId(shardId);

        return mapper.selectOne(condition);
    }
}
